//
//  XPTextModifyView.swift
//  PPKit
//
//  Created by xiaopin on 2019/9/10.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import UIKit

typealias XPTextModifyViewOkClosure = (_ value:String)->Void

/// 文本修改视图
class XPTextModifyView: XPBaseView,UITextFieldDelegate {

    //MARK: 属性
    
    var okClosure:XPTextModifyViewOkClosure?
    
    /// 透明背景视图
    lazy var alphaView: UIView = {
        let view = UIView(frame: pp_frame(0,0,pp_screen_width,pp_screen_height))
        view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        view.alpha = 0
        return view
    }()
    
    //内容视图
    lazy var contentView: UIView = {
        let view = UIView(frame: pp_frame(0,pp_screen_height,pp_screen_width,200))
        view.backgroundColor = .white
        view.cornerRadius = 5
        return view
    }()
    
    //导航栏
    lazy var navView: UIView = {
        let view = UIView(frame: pp_frame(0,0,pp_screen_width,44))
        view.backgroundColor = .white
        view.addSubview(labTitle)
        view.addSubview(btnClose)
        view.addSubview(btnOK)
        view.addSubview(navLine)
        return view
    }()
    
    //关闭按钮
    lazy var btnClose: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = pp_frame(0, 0,44, 44)
        button.setImage(UIImage(named:"close_7"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
        return button
    }()
    
    //标题
    lazy var labTitle: UILabel = {
        let label = UILabel(frame: pp_frame(0,0,self.width,44))
        label.text = "标题"
        label.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        label.font = .systemFont(ofSize: 17)
        label.textAlignment = .center
        return label
    }()
    
    //OK
    lazy var btnOK: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = pp_frame(self.width - 54, 7, 44, 30)
        button.setTitle("确定", for: .normal)
        button.setTitleColor(#colorLiteral(red: 0.3921568627, green: 0.3921568627, blue: 0.3921568627, alpha: 1), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        button.addTarget(self, action: #selector(okAction), for: .touchUpInside)
        button.cornerRadius = 5
        button.borderColor = Color_Gray3
        return button
    }()
    
    //导航栏分割线
    lazy var navLine: UIView = {
        let view = UIView(frame: pp_frame(0,44,self.width,0.5))
        view.backgroundColor = Color_Gray2
        return view
    }()
    
    //文本框
    lazy var textBox: UITextField = {
        let text = UITextField(frame: pp_frame(13,44+15,self.width-26,30))
        text.placeholder = "替换内容"
        text.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        text.font = UIFont.systemFont(ofSize: 14)
        text.clearButtonMode = UITextField.ViewMode.whileEditing
        text.returnKeyType = .done
        text.delegate = self
        return text
    }()
    
    //文本框分割线
    lazy var textLine: UIView = {
        let view = UIView(frame: pp_frame(13,textBox.top+textBox.height + 3,textBox.width,0.5))
        view.backgroundColor = Color_Gray2
        return view
    }()
    
    //MARK: 重载父类方法
    /// 初始化数据
    override func initData() {
        
    }
    
    /// 初始化当前视图的子控件
    override func initSubViews() {
        self.addSubview(self.alphaView)
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.navView)
        self.contentView.addSubview(self.textBox)
        self.contentView.addSubview(self.textLine)
    }
    
    //MARK: 自定义方法
    func showContent(_ title:String? = "",_ placeholder:String? = "",_ value:String? = "",_ keyboardType:UIKeyboardType = .default){
        
        self.labTitle.text = title
        self.textBox.text = value
        self.textBox.placeholder = placeholder
        self.textBox.keyboardType = keyboardType
        
        UIView.animate(withDuration: 0.3, animations: {
            self.alphaView.alpha = 1
        }, completion: { (finished) in
            UIView.animate(withDuration: 0.3, animations: {
                self.contentView.top = pp_screen_height - pp_screen_navbar_height - self.contentView.height
            })
        })
    }
    
    //MARK: 事件通知
    
    func closeAction(){
        UIView.animate(withDuration: 0.3, animations: {
            self.contentView.top = pp_screen_height
        }, completion: { (finished) in
            UIView.animate(withDuration: 0.3, animations: {
                self.alphaView.alpha = 0
                self.removeFromSuperview()
            })
        })
    }
    
    func okAction(){
        if let closure = okClosure {
            closure(textBox.text!)
        }
        closeAction()
    }
    
    //MARK: 委托代理
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
